import "reflect-metadata"
import "./common";

events.removeAllListeners()
events.setMaxListeners(1000)

import { container as injector } from 'tsyringe'
/**
 * 制作一个APP管理框架
 */
import { Timer } from "./autox/timer";
import { CircularTaskManager } from "./v2/node";
import { FloatyWindow } from "./autox/floatWindow";
import { KwaiZhaoBaoSheduleTask } from "./v2/tasks/KwaiZhaoBaoSheduleTask";
import { KwaiQiangBaoSheduleTask } from "./v2/tasks/KwaiQiangBaoSheduleTask";
import { KwaiNebulaZhaoBaoSheduleTask } from "./v2/tasks/KwaiNebulaZhaoBaoSheduleTask";
import { KwaiNebulaQiangBaoSheduleTask } from "./v2/tasks/KwaiNebulaQiangBaoSheduleTask";
import { KwaiNebulaYuEScheduleTask } from "./v2/tasks/KwaiNebulaYuEScheduleTask";
import { KwaiYuEScheduleTask } from "./v2/tasks/KwaiYuEScheduleTask";
import { httpGet } from "./autox/http";
import { Config } from "./autox/config";
const config = injector.resolve(Config)
const timer = injector.resolve(Timer)

declare const runtime: any;

function safeRun(fn: Function) {
    try {
        fn()
    } catch (e) {
        log(`SafeRun:Error ${e.message}`)
    }
}
timer.setInterval(() => { }, 1000)
events.on(`exit`, () => {
    safeRun(() => console.hide())
    safeRun(() => events.removeAllListeners())
    safeRun(() => injector.dispose())
    safeRun(() => config.remove(`MINE_TASK`))
});

export const start = () => {
    render()
    runtime.info.useShell = true;
    const taskManager = injector.resolve(CircularTaskManager)
    // if (Math.random() > 0.5) {
    //     taskManager.add(new KwaiNebulaYuEScheduleTask())
    //     taskManager.add(new KwaiYuEScheduleTask())
    // } else {
    //     taskManager.add(new KwaiYuEScheduleTask())
    //     taskManager.add(new KwaiNebulaYuEScheduleTask())
    // }
    // if (Math.random() > 0.5) {
    // taskManager.add(new KwaiZhaoBaoSheduleTask())
    // taskManager.add(new KwaiNebulaZhaoBaoSheduleTask())
    // } else {
    // taskManager.add(new KwaiNebulaZhaoBaoSheduleTask())
    // taskManager.add(new KwaiZhaoBaoSheduleTask())
    // }
    if (Math.random() > 0.5) {
        taskManager.add(new KwaiQiangBaoSheduleTask())
        // taskManager.add(new KwaiNebulaQiangBaoSheduleTask())
    } else {
        // taskManager.add(new KwaiNebulaQiangBaoSheduleTask())
        taskManager.add(new KwaiQiangBaoSheduleTask())
    }
    taskManager.run()
}

function render(): void {
    const floatWindow = injector.resolve(FloatyWindow)
    floatWindow.render(`<frame id="main" w="*" h="*" bg="#000000" alpha="0">
    <vertical w="*" h="*">
        <horizontal padding="0 0 0 0">
            <text w="*" padding="15 5 15 5" bg="#130B2E" h="auto" id="toast" textSize="16" gravity="center" text="" textColor="#D12E2E"/>
        </horizontal>
        <horizontal padding="15 5 15 5">
            <text w="auto" textSize="12" gravity="left" text="当前任务：" textColor="#E2E1CF"/>
            <text w="*" id="currentTask" textSize="14" gravity="left" text="-" textColor="#F2F2F2"/>
        </horizontal>
        <horizontal padding="15 5 15 5">
            <text w="auto" textSize="12" gravity="left" text="执行结果：" textColor="#F2F2F2"/>
            <text w="*" id="currentResult" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
        </horizontal>
        <horizontal padding="15 5 15 5">
            <horizontal layout_weight="1">
                <text w="auto" textSize="12" gravity="left" text="快手：" textColor="#F2F2F2"/>
                <text w="*" id="kwaiCurrentMoney" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
            </horizontal>
            <horizontal layout_weight="1">
                <text w="auto" textSize="12" gravity="left" text="分配/抓取：" textColor="#F2F2F2"/>
                <text w="*" id="kwaiTodayCount" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
            </horizontal>
            <horizontal layout_weight="1">
                <text w="auto" textSize="12" gravity="left" text="快币：" textColor="#F2F2F2"/>
                <text w="*" id="kwaiTodayMoney" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
            </horizontal>
        </horizontal>
        <horizontal padding="15 5 15 5">
            <horizontal layout_weight="1">
                <text w="auto" textSize="12" gravity="left" text="极速：" textColor="#F2F2F2"/>
                <text w="*" id="nebulaCurrentMoney" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
            </horizontal>
            <horizontal layout_weight="1">
                <text w="auto" textSize="12" gravity="left" text="分配/抓取：" textColor="#F2F2F2"/>
                <text w="*" id="nebulaTodayCount" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
            </horizontal>
            <horizontal layout_weight="1">
                <text w="auto" textSize="12" gravity="left" text="快币：" textColor="#F2F2F2"/>
                <text w="*" id="nebulaTodayMoney" textSize="14" gravity="left" text="-" textColor="#E2E1CF"/>
            </horizontal>
        </horizontal>
        <horizontal padding="15 5 15 5">
            <text w="*" id="tip" textSize="12" gravity="center" text="-" textColor="#f29a8c"/>
        </horizontal>
        <horizontal h="50" visibility="invisible" >
            <input id="input" />
        </horizontal>
    </vertical>
</frame>`)
const task = threads.start(() => {
    ui.run(() => {
        const parentParent = this.window.main.parent.parent.parent;
        let params = parentParent.layoutParams || {};
        if (params.flags == 16777736) {
            log("当前可以触摸, 修改为不可触摸");
            params.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        } else if (params.flags == 16777752) {
            log("当前不可以触摸, 修改为可触摸");
            params.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        }
        const windowManager = context.getSystemService(context.WINDOW_SERVICE);
        windowManager.updateViewLayout(parentParent, params);
        const w = this.window;
        let wh = w.getHeight();
        let dw = device.width;
        let dh = device.height;
        w.setSize(dw + 100, wh);
        w.main.attr('alpha', 1)
        // 悬浮窗置于底部中央
        let y = dh - wh;
        w.setPosition(0, y);
    })
})
    const updateUi = (state: any) => {
        ui.run(() => {
            Object.keys(state).map(key => {
                const val = state[key]
                if (val) {
                    floatWindow.window[key].setText(state[key])
                } else {
                    log(`${key}:${val}`)
                }
            })
        })
    }
    floatWindow.on('UPDATE_FLOAT_WINDOW_STATE_CHANGE', (val: any) => {
        const storage: any = config.get(`MINE_TASK`) || {}
        const state = {
            currentTask: '抢包',
            ...storage,
            ...val,
        };
        config.set(`MINE_TASK`, state)
        updateUi(state)
    });
}

start()